package com.flexcloudsdk.taskqueue;

import java.util.Map;
import java.util.logging.Logger;
import com.google.appengine.api.taskqueue.DeferredTask;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;
import com.google.appengine.api.taskqueue.TaskOptions.Builder;
import com.google.appengine.api.taskqueue.RetryOptions;

public class CloudTaskOptions {
	private static final Logger log = Logger.getLogger(CloudTaskOptions.class.getName());
	private TaskOptions taskOptions = null;
	public static Method CloudMethod;
	private String sourceMethod;
	private String sourceClass;
				
	public CloudTaskOptions() {
		log.info("CloudTaskOptions has been created by BlazeDS");
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		sourceClass = CloudTaskOptions.class.getName();
	}
		
	public CloudTaskOptions(TaskOptions options) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		sourceClass = CloudTaskOptions.class.getName();
		
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = options;
	
	}

	public static CloudTaskOptions createWithCountdownMillis(long countdownMillis) {
		log.info("CloudTaskOptions.createWithCountdownMillis with countdownMillis: " + String.valueOf(countdownMillis) );

		TaskOptions value = Builder.withCountdownMillis(countdownMillis);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	 
	public static CloudTaskOptions createWithDefaults() {
		log.info("Executing: " + "'CloudTaskOptions.createWithDefaults");
		
		TaskOptions value = Builder.withDefaults();
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithEtaMillis(long etaMillis) {
		log.info("CloudTaskOptions.createWithEtaMillis with etaMillis: " + String.valueOf(etaMillis) );
		
		TaskOptions value = Builder.withEtaMillis(etaMillis);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithHeader(String header, String headerValue) {
		log.info("CloudTaskOptions.createWithHeader with header,headerValue: " + header + "," + headerValue );
		
		TaskOptions value = Builder.withHeader(header, headerValue);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithHeaders(Map<String, String> headers) {
		log.info("CloudTaskOptions.createWithHeaders with headers: " + headers.toString() );
		
		TaskOptions value = Builder.withHeaders(headers);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithMethod(TaskOptions.Method myMethod) {
		log.info("CloudTaskOptions.createWithMethod with TaskOptions.Method: " + myMethod.toString() );
	
		TaskOptions value = Builder.withMethod(myMethod);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithParam(String param, byte[] data) {
		log.info("CloudTaskOptions.createWithParam with param,data: " + param + "," + data.toString() );

		TaskOptions value = Builder.withParam(param, data);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithParam(String paramName, String paramValue) {
		log.info("CloudTaskOptions.createWithParam with paramName,paramValue: " + paramName + "," + paramValue );

		TaskOptions value = Builder.withParam(paramName, paramValue);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithPayload(byte[] payload, String contentType) {
		log.info("CloudTaskOptions.createWithPayload with payload,contentType: " + payload.toString() + "," + contentType );

		TaskOptions value = Builder.withPayload(payload, contentType);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}

	public static  CloudTaskOptions createWithPayLoad(DeferredTask deferredTask) {
		log.info("CloudTaskOptions.createWithPayLoad with DeferredTask: " + deferredTask.toString());

		TaskOptions value = Builder.withPayload(deferredTask);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithPayLoad(String payload) {
		log.info("CloudTaskOptions.createWithPayLoad with payload: " + payload);
		
		TaskOptions value = Builder.withPayload(payload);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithPayload(String payload, String charset) {
		log.info("CloudTaskOptions.createWithPayload with payload,charset: " + payload + "," + charset);
		
		TaskOptions value = Builder.withPayload(payload, charset);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithRetyOptions(RetryOptions options) {
		log.info("CloudTaskOptions.createWithRetyOptions with retryoptions: " + options.toString());
		
		TaskOptions value = Builder.withRetryOptions(options);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithTaskName(String name) {
		log.info("CloudTaskOptions.createWithTaskName with name: " + name);
		
		TaskOptions value = Builder.withTaskName(name);
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public static CloudTaskOptions createWithUrl(String url) {
		log.info("CloudTaskOptions.createWithUrl with URL: " + url);
		TaskOptions value = Builder.withUrl(url) ;
		CloudTaskOptions cloudOptions = new CloudTaskOptions(value);
		return cloudOptions;
	}
	
	public CloudTaskOptions clearParams() {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = taskOptions.clearParams();
		return this;
	}

	public CloudTaskOptions get(long countdownMillis) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = taskOptions.countdownMillis(countdownMillis);
		return this;
	}
	
	public CloudTaskOptions etaMillis(long etaMillis) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = taskOptions.etaMillis(etaMillis);
		return this;
	}
	
	public String getUrl() {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		return taskOptions.getUrl();
	}

	@Override
	public int hashCode() {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		return taskOptions.hashCode();
	}
	
	public CloudTaskOptions header(String headerName, String value) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = taskOptions.header(headerName, value);
		return this;
	}
	
	public CloudTaskOptions  headers(Map<String, String> headers) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = taskOptions.headers(headers);
		return this;
	}
	
	public CloudTaskOptions method(TaskOptions.Method method) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = taskOptions.method(method);
		return this;
	}
	
	public CloudTaskOptions param(String name, byte[] value) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = taskOptions.param(name, value);
		return this;
	}
	
	public CloudTaskOptions parm(String name, String value) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		taskOptions = taskOptions.param(name, value);
		return this;
	}
	
	public CloudTaskOptions payload(byte[] value) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		taskOptions = taskOptions.payload(value);

		return this;
	}
	
	public CloudTaskOptions payload(byte[] value, String content) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		taskOptions = taskOptions.payload(value, content);

		return this;
	}
	
	public CloudTaskOptions removeHeader(String headerName) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		taskOptions = taskOptions.removeHeader(headerName);

		return this;
	}
	
	public CloudTaskOptions removeParam(String paramName) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		taskOptions = taskOptions.removeParam(paramName);

		return this;
	}
	
	public CloudTaskOptions retryOptions(RetryOptions retryOptions) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		taskOptions = taskOptions.retryOptions(retryOptions);
		
		log.exiting(sourceClass, sourceMethod);
		return this;
	}
	
	public CloudTaskOptions taskName(String taskName) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		
		taskOptions = taskOptions.taskName(taskName);
		
		return this;
	}
	
	@Override
	public String toString() {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return taskOptions.toString();
	}
	
	public CloudTaskOptions  url(String url) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
				
		taskOptions = taskOptions.url(url);
		
		return this;
	}
	
}
	
